package com.supermap.dataservice.grid.griddata.util;

import java.util.List;

import com.google.common.primitives.Doubles;
import com.google.common.primitives.Floats;
import com.google.common.primitives.Ints;

/**  
 * @Description: 数组的操作工具类
 * @author zhoujian
 * @date 2018-5-28
 * @version V1.0 
 */
public class ArrayUtil
{
	/**
	 * 
	 * @Description: 获取数据中最大值
	 * @return float
	 * @throws
	 */
	public static float max(float[] array)
	{
		return Floats.max(array);
	}
	/**
	 * 
	 * @Description: 获取数据中最大值
	 * @return float
	 * @throws
	 */
	public static float max(float[][] array) throws Exception
	{
		if(array == null)
		{
			throw new Exception("参数数组不能为空!");
		}
		float[] maxArray = new float[array.length];
		for (int i = 0; i < array.length; i++) {
			float max = Floats.max(array[i]);
			maxArray[i] = max;
		}
		
		return Floats.max(maxArray);
	}
	/**
	 * 
	 * @Description: 获取数据中最大值
	 * @return int
	 * @throws
	 */
	public static int max(int[] array)
	{
		return Ints.max(array);
	}
	
	/**
	 * 
	 * @Description: 获取数据中最大值
	 * @return double
	 * @throws
	 */
	public static double max(double[] array)
	{
		return Doubles.max(array);
	}
	
	/**
	 * 
	 * @Description: 获取数据中最小值
	 * @return float
	 * @throws
	 */
	public static float min(float[] array)
	{
		return Floats.min(array);
	}
	/**
	 * 
	 * @Description: 获取数据中最小值
	 * @return float
	 * @throws
	 */
	public static float min(float[][] array) throws Exception
	{
		if(array == null)
		{
			throw new Exception("参数数组不能为空!");
		}
		float[] minArray = new float[array.length];
		for (int i = 0; i < array.length; i++) {
			float max = Floats.min(array[i]);
			minArray[i] = max;
		}
		
		return Floats.max(minArray);
	}
	/**
	 * 
	 * @Description: 获取数据中最小值
	 * @return int
	 * @throws
	 */
	public static int min(int[] array)
	{
		return Ints.min(array);
	}
	
	/**
	 * 
	 * @Description: 获取数据中最小值
	 * @return double
	 * @throws
	 */
	public static double min(double[] array)
	{
		return Doubles.min(array);
	}

	/**
	 * 
	 * @Description: 判断数组中是否包含目标值
	 * @return boolean
	 * @throws
	 */
	public static boolean contains(int[] array , int target)
	{
		return Ints.contains(array, target);
	}
	
	/**
	 * 
	 * @Description: 判断数组中是否包含目标值
	 * @return boolean
	 * @throws
	 */
	public static boolean contains(float[] array , float target)
	{
		return Floats.contains(array, target);
	}
	
	/**
	 * 
	 * @Description: 判断数组中是否包含目标值
	 * @return boolean
	 * @throws
	 */
	public static boolean contains(double[] array , double target)
	{
		return Doubles.contains(array, target);
	}
	
	/**
	 * 
	 * @Description: 一维数组转换成二维数组
	 * @param data 一维数组数据 
	 * @param rows 行数
	 * @param cols 列数
	 * @author zhoujian
	 * @return [][]
	 * @throws
	 */
	public static float[][] arrayTwoDimensionalArray(float[] array, int rows, int cols) throws Exception
	{
		float[][] newArray = new float[rows][cols];
		
		for (int i = 0; i < rows; i++)
		{
			System.arraycopy(array, i * cols, newArray[i], 0, cols);
		}
		return newArray;
	}
	public static void main(String[] args) throws Exception
	{
		float[][] test = new float[3][4];
		for (int i = 0; i < test.length; i++)
		{
			for (int j = 0; j < test[0].length; j++)
			{
				test[i][j] = i * 4 + j;
				System.out.println(test[i][j]);
			}
		}
		
		float[] test2 = arrayConvert2DTo1D(test);
		System.out.println();
	}
	/**
	 * 二维数组转换成一维数组
	 * @param array2D 源数组
	 * @return float[] 目标数组
	 */
	public static float[] arrayConvert2DTo1D(float[][] array2D) throws Exception
	{
		if (array2D == null)
		{
			throw new Exception("转换数组不能为空。");
		}
		int rows = array2D.length;
		int clos = array2D[0].length;
		int length = rows * clos;
		float[] resultArray = new float[length];
		
		try
		{
			for (int i = 0; i < rows; i++)
			{
				System.arraycopy(array2D[i], 0, resultArray, i * clos, clos);
			}
		}
		catch (Exception e)
		{
			throw new Exception("二维数组转换成一维数组异常，异常信息为：" + e.getMessage());
		}
		
		return resultArray;
	}
	
}
